//
//  SHSessionViewController.m
//  Netease
//
//  Created by 王方伟 on 2017/12/21.
//  Copyright © 2017年 wfw. All rights reserved.
//

#import "SHSessionViewController.h"
#import "SHSessionInteractorImpl.h"
#import "SHSessionInteractor.h"
#import "UIImage+Extension.h"
#import "SHSessionConfig.h"
#import "UIImage+NIMKit.h"

@interface SHSessionViewController ()
@property (strong, nonatomic) SHSessionConfig *sessionConfig;
@property (nonatomic,strong) id<SHSessionInteractor> customInteractor;
@end

@implementation SHSessionViewController

- (id<NIMSessionConfig>)sessionConfig{
    if (_sessionConfig == nil) {
        _sessionConfig = [[SHSessionConfig alloc] init];
        _sessionConfig.session = self.session;
    }
    return _sessionConfig;
}

- (id<SHSessionInteractor>)customInteractor{
    if (_customInteractor == nil) {
        _customInteractor = [[SHSessionInteractorImpl alloc] initWithSession:self.session config:self.sessionConfig];
    }
    return _customInteractor;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupCellStyle];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    NIMInputToolBar *toolBar = self.sessionInputView.toolBar;
    toolBar.recordButton.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [toolBar.recordButton setTitleColor:SHColor(71,86,105) forState:UIControlStateNormal];
}

#pragma mark 设置Cell样式
- (void)setupCellStyle{
    NIMKitConfig *config = [NIMKit sharedKit].config;
    config.avatarType = NIMKitAvatarTypeRounded;
    config.placeholder = @"请输入消息";
    
    NIMKitSettings *leftSetting = config.leftBubbleSettings;
    leftSetting.imageSetting.normalBackgroundImage = nil;
    leftSetting.imageSetting.highLightBackgroundImage = nil;
    
    leftSetting.videoSetting.normalBackgroundImage = nil;
    leftSetting.videoSetting.highLightBackgroundImage = nil;
    
    leftSetting.textSetting.contentInsets = UIEdgeInsetsMake(10, 15, 10, 15);
    leftSetting.textSetting.font = [UIFont systemFontOfSize:16];
    leftSetting.textSetting.textColor = SHColor(71,86,105);
    
    
    NIMKitSettings *rightSetting = config.rightBubbleSettings;
    rightSetting.textSetting.normalBackgroundImage    =  [[UIImage nim_imageInKit:@"icon_sender_text_node_normal"] resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{4,4,4,4}") resizingMode:UIImageResizingModeStretch];
    rightSetting.textSetting.highLightBackgroundImage =  [[UIImage nim_imageInKit:@"icon_sender_text_node_pressed"] resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{4,4,4,4}") resizingMode:UIImageResizingModeStretch];
    
    rightSetting.imageSetting.normalBackgroundImage = nil;
    rightSetting.imageSetting.highLightBackgroundImage = nil;
    
    rightSetting.videoSetting.normalBackgroundImage = nil;
    rightSetting.videoSetting.highLightBackgroundImage = nil;
    
    rightSetting.textSetting.contentInsets = UIEdgeInsetsMake(10, 15, 10, 9);
    rightSetting.textSetting.font = [UIFont systemFontOfSize:16];
}

#pragma mark 更多菜单配置
#pragma mark 选择相册
- (void)onTapMediaItemPicture:(NIMMediaItem *)item{
    [self.customInteractor mediaPicturePressed];
}

#pragma mark 选择拍摄
- (void)onTapMediaItemShoot:(NIMMediaItem *)item{
    [self.customInteractor mediaShootPressed:self.session];
}

#pragma mark 选择位置
- (void)onTapMediaItemLocation:(NIMMediaItem *)item{
    [self.customInteractor mediaLocationPressed];
}



#pragma mark Cell事件
- (BOOL)onTapCell:(NIMKitEvent *)event{
    BOOL handled = [super onTapCell:event];
    NSString *eventName = event.eventName;
    if ([eventName isEqualToString:NIMKitEventNameTapContent]){
        NIMMessage *message = event.messageModel.message;
        NSDictionary *actions = [self cellActions];
        NSString *value = actions[@(message.messageType)];
        if (value) {
            SEL selector = NSSelectorFromString(value);
            if (selector && [self respondsToSelector:selector]) {
                SuppressPerformSelectorLeakWarning([self performSelector:selector withObject:message]);
                handled = YES;
            }
        }
    }
    return handled;
}


#pragma mark 查看图片
- (void)showImage:(NIMMessage *)message{
    [self.customInteractor showImage:message session:self.session];
}


#pragma mark 查看地理位置
- (void)showLocation:(NIMMessage *)message{
    [self.customInteractor showLocation:message];
}

#pragma mark 查看视频
- (void)showVideo:(NIMMessage *)message{
    [self.customInteractor showVideo:message session:self.session];
}

#pragma mark 查看文件
- (void)showFile:(NIMMessage *)message{
    [self.customInteractor showFile:message];
}

- (NSDictionary *)cellActions{
    static NSDictionary *actions = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        actions = @{@(NIMMessageTypeImage) :    @"showImage:",
                    @(NIMMessageTypeVideo) :    @"showVideo:",
                    @(NIMMessageTypeLocation) : @"showLocation:",
                    @(NIMMessageTypeFile)  :    @"showFile:",
                    @(NIMMessageTypeCustom):    @"showCustom:"};
    });
    return actions;
}



@end
